package com.ass.freedom.net.handler;

import com.alibaba.fastjson.JSONObject;
import com.ass.freedom.net.protocol.FMTraits;
import com.ass.freedom.net.protocol.FMessage;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.ScheduledFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author syq
 * @date 2019/12/24
 */
@Slf4j
@ChannelHandler.Sharable
public class ServerHandler extends FMessageHandler {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("channel connected: {}", ctx.channel().remoteAddress().toString());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("channel closed: {}", ctx.channel().remoteAddress().toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("channel {}", ctx.channel().remoteAddress().toString());
        log.error("", cause);
    }

    @Override
    protected void onPing(ChannelHandlerContext ctx, FMessage msg) throws Exception {
        super.onPing(ctx, msg);
        log.info("onPing");
    }

    @Override
    protected void onRequest(ChannelHandlerContext ctx, FMessage msg) throws Exception {
        log.info("onRequest: '{}'", msg.toString());
        FMessage res = null;
        if (FMTraits.STRING == msg.getContentType()) {
            String content = (String) msg.getContent();
            content = content + "-response";
            res = FMessage.buildResponse(msg, content);
        } else if (FMTraits.JSON == msg.getContentType()) {
            JSONObject json = (JSONObject) msg.getContent();
            json.put("response", 1);
            res = FMessage.buildResponse(msg, json);
        } else if (FMTraits.PROTOBUF == msg.getContentType()) {
//            PBContent.ProtoContent pbContent = (PBContent.ProtoContent) msg.getContent();
//            if (1 == pbContent.getCmd()) {
//                PBContent.GetPersonInfoReq pbReq = PBContent.GetPersonInfoReq.parseFrom(pbContent.getData());
//                log.info("pbReq: '{}'", pbReq.toString());
//            }
//
//            PBContent.PersonInfoRes pbRes = PBContent.PersonInfoRes.newBuilder()
//                    .setArea(2)
//                    .setPersonId("212121")
//                    .setName("Tom")
//                    .setInterest("music")
//                    .build();
//            pbContent = PBContent.ProtoContent.newBuilder()
//                    .setCmd(1)
//                    .setErrCode(0).setErrMsg("OK")
//                    .setData(pbRes.toByteString())
//                    .build();
//            res = FMessage.buildResponse(msg, pbContent);
        }

        if (null != res) {
            ctx.channel().writeAndFlush(res);
        }

    }

}
